package com.biter;

import geniuz.myPathbutton.composerLayout;

import java.util.Date;
import java.util.List;

import cn.waps.AdView;
import cn.waps.AppConnect;

import com.biter.bean.KeyValueBean;
import com.biter.bean.MarketItemBean;
import com.biter.config.ConfigDef;
import com.biter.service.PollingService;
import com.biter.util.StringHelper;
import com.biter.util.Util;
import com.wanpu.pay.PayConnect;
import com.wanpu.pay.PayResultListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MainActivity extends Activity {
    private final String TAG = "MainActivity";
	private List marketDataV = null;
	private MarketItemAdapter mtaAdapter =null;
	private MarketTickerReceiver rTickerReceiver = null;
	private ListView marketListView = null;
	private Context context = null;
	private Handler myHandler = new Handler(){
		public void handleMessage(Message msg){
			switch (msg.what) {
			case 1:
				Bundle bundle = msg.getData();
				KeyValueBean[] valueBeans = ConfigDef.marketList;
				for(KeyValueBean kvBean:valueBeans){
					String marketName = kvBean.getValueAtIdx(2);
					String jsonData = bundle.getString(marketName);
					View itemView = (View)marketListView.findViewWithTag(marketName);
					MarketItemBean mibBean =  new MarketItemBean(itemView);
					mibBean.setJsonData(jsonData,marketName);	
				}
				mtaAdapter.notifyDataSetChanged();
			default:
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		context = this;
		AppConnect.getInstance(this);	
		if(Util.checkShowAd(context)) {
			LinearLayout container =(LinearLayout)findViewById(R.id.AdLinearLayout);
			new AdView(this,container).DisplayAd();
		    AppConnect.getInstance(this).initPopAd(this);
		}
		PayConnect.getInstance("3f0d31d73bc6953889768c6d6c4f33a1", "gfan", this);
		
		if(!Util.isServiceRunning(this, PollingService.ACTION)){
             Intent intent = new Intent("startPollService");
             sendBroadcast(intent);
		}
				
		KeyValueBean[] valueBeans = ConfigDef.marketList;
		marketListView = (ListView)findViewById(R.id.marketList);
		mtaAdapter = new MarketItemAdapter(this,valueBeans,marketListView);
		marketListView.setAdapter(mtaAdapter);
		marketListView.setOnItemClickListener(new itemClick(this));
		rTickerReceiver = new MarketTickerReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.biter.marketticker");
		this.registerReceiver(rTickerReceiver, filter);
		
		composerLayout clayout = (composerLayout) findViewById(R.id.pathMenu);
		clayout.init(new int[] { R.drawable.composer_user,
				R.drawable.composer_transaction, R.drawable.composer_wei,
				R.drawable.composer_sell,R.drawable.composer_buy }, R.drawable.composer_button,
				R.drawable.composer_icn_plus, composerLayout.LEFTBOTTOM, 180,
				300);
		clayout.setButtonsOnClickListener(new ButtonsClick(this));
	}

	
	private class MarketTickerReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String actionName = intent.getAction();
			if(actionName.equals("com.biter.marketticker")){
			      Bundle bundle = intent.getExtras();
			      Message msg = new Message();
			      msg.what = 1;
			      msg.setData(bundle);
			      myHandler.sendMessage(msg);
			}
		}
	}
	
	private class itemClick implements OnItemClickListener{
        private Context context = null;
		public itemClick(Context context){
			this.context = context;
		}
		@Override
		public void onItemClick(AdapterView<?> arg0, View itemView, int arg2,
				long arg3) {
             Log.v(TAG, String.valueOf(itemView.getTag()));
             Intent intent = new Intent(MainActivity.this,QuotationActivity.class);
             context.startActivity(intent);
		}
		
	}
	
	private class ButtonsClick implements OnClickListener{

        private Context context = null;
		public ButtonsClick(Context context){
			this.context = context;
		}
		@Override
		public void onClick(View view) {
			if(view.getId()==104){
	             Intent intent = new Intent(MainActivity.this,BuyBTCActivity.class);
	             context.startActivity(intent);
			}
			Log.v(TAG, String.valueOf(view.getId()));		
		}
		
	}
	
	@Override
	protected void onDestroy() {
		this.unregisterReceiver(rTickerReceiver);
		AppConnect.getInstance(this).close();
		PayConnect.getInstance(context).close();
		super.onDestroy();
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			Intent intent = new Intent(context,SettingActivity.class);
			this.startActivity(intent);
			break;
		case R.id.action_pays:
            Util.show_pay(context);
			break;
		default:
			break;
		}

		return super.onMenuItemSelected(featureId, item);
	}
	
	

}
